<?php

class Sblog_IndexController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
    $this->view->someVar = 'someVal';
  }
  public function browseAction()
  {
    $this->view->someVar = 'browse';
    $values = $this->_getAllParams();
    $paginator = Engine_Api::_()->sblog()->getBlogsPaginator($values);
    $this->view->paginator = $paginator; 
  }
  public function createAction()
  {
 
    $this->view->form = $form = new Sblog_Form_Create();
    
    // If not post or form not valid, return
    if( !$this->getRequest()->isPost() ) {
      return;
    }
    if( !$form->isValid($this->getRequest()->getPost()) ) {
      return;
    }
    
    $values = $form->getValues();
    Engine_Api::_()->getApi('behavior', 'sblog')->create($values);
     

  }
  public function deleteAction()
  {
  	$sblog_id = $this->_getParam('sblog_id');
 	
    $this->view->form = $form = new Sblog_Form_Delete();
    $check_before_delete = Engine_Api::_()->getApi('behavior', 'sblog')->checkBeforeDelete($sblog_id);
    if( !$check_before_delete )
    {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_("Blog entry doesn't exist or not authorized to delete");
      return;
    }
    
    if( !$this->getRequest()->isPost() )
    {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
      return;
    }
    Engine_Api::_()->getApi('behavior', 'sblog')->delete($sblog_id);

    $this->view->status = true;
    $this->view->message = Zend_Registry::get('Zend_Translate')->_('Your blog entry has been deleted.');
    return $this->_forward('success' ,'utility', 'core', array(
      'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'index'), 'sblog_general', true),
      'messages' => Array($this->view->message)
    ));
  }
  public function editAction()
  {
  	$sblog_id = $this->_getParam('sblog_id');
  	$values = Engine_Api::_()->getApi('behavior', 'sblog')->getBlogEntryValue($sblog_id);
    $this->view->form = $form = new Sblog_Form_Edit();
    
    // Populate form
    $form->populate($values);
    
    // Check post/form
    if( !$this->getRequest()->isPost() ) {
      return;
    }
    if( !$form->isValid($this->getRequest()->getPost()) ) {
      return;
    }
    $values = $form->getValues();
    Engine_Api::_()->getApi('behavior', 'sblog')->edit($values,$sblog_id);
   
  }
  public function viewAction(){
  	$sblog_id = $this->_getParam('sblog_id'); 
  	$sblog    = Engine_Api::_()->getItem('sblogs',$sblog_id);
    $this->view->blog =  $sblog ;
    $this->view->owner = $user = $this->view->blog->getOwner();
    
    if( $sblog ){
     // Engine_Api::_()->core()->setSubject($sblog);
      
    }
   // var_dump($sblog->authorization()->isAllowed($viewer, 'comment'));
  }
}
